package com.enjoyor.soa.traffic.rest.efoms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.efoms.WorkordersInfoDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.rest.efoms.dubbo.IWorkordersInfoConsumeService;
import com.enjoyor.soa.traffic.rest.efoms.enums.EnumDicPublicRest;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/workorders" })
@Api("维修工单rest接口")
@CrossOrigin
public class WorkordersInfoController {
	@Autowired
	private IWorkordersInfoConsumeService workordersInfoConsumeService;

	@GetMapping(value = { "getWorkordersInfoById" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("根据编号查询维修工单信息")
	@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String")
	public ResultPojo getWorkordersInfoById(
			@RequestParam(defaultValue = "", value = "workordersId", required = false) String workordersId,
			HttpServletRequest request) {
		return workordersInfoConsumeService.selectWorkordersInfoById(workordersId);
	}
	
	@GetMapping(value = { "getWorkordersInfoList" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("查询维修工单列表信息")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repStartDate", value = "报修开始时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repEndDate", value = "报修结束时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devCategoryCode", value = "设备种类编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devTypeCode", value = "设备类型编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devId", value = "设备编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devName", value = "设备名称", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "key", value = "关键字（设备编号或设备名称）", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devAreaCode", value = "设备所属区域", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devDeptId", value = "设备所属部门", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "failureTypeCode", value = "故障类型编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "failureDescrible", value = "故障描述", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repSourceCode", value = "报修来源编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "workordersStatusCode", value = "工单状态编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "lableClick", value = "标签勾选(催办press、延期postpone、逾期overtime、进度反馈fackback、驳回isreject)", 
							paramType = "query", dataType = "String") })
	public ResultPojo getWorkordersInfoList(
			@RequestParam(defaultValue = "", value = "workordersId", required = false) String workordersId,
			@RequestParam(defaultValue = "", value = "repStartDate", required = false) String repStartDate,
			@RequestParam(defaultValue = "", value = "repEndDate", required = false) String repEndDate,
			@RequestParam(defaultValue = "", value = "devCategoryCode", required = false) String devCategoryCode,
			@RequestParam(defaultValue = "", value = "devTypeCode", required = false) String devTypeCode,
			@RequestParam(defaultValue = "", value = "devId", required = false) String devId,
			@RequestParam(defaultValue = "", value = "devName", required = false) String devName,
			@RequestParam(defaultValue = "", value = "key", required = false) String key,
			@RequestParam(defaultValue = "", value = "devAreaCode", required = false) String devAreaCode,
			@RequestParam(defaultValue = "", value = "devDeptId", required = false) String devDeptId,
			@RequestParam(defaultValue = "", value = "failureTypeCode", required = false) String failureTypeCode,
			@RequestParam(defaultValue = "", value = "failureDescrible", required = false) String failureDescrible,
			@RequestParam(defaultValue = "", value = "repSourceCode", required = false) String repSourceCode,
			@RequestParam(defaultValue = "", value = "workordersStatusCode", required = false) String workordersStatusCode,
			@RequestParam(defaultValue = "", value = "lableClick", required = false) String lableClick,
			HttpServletRequest request) {
		return workordersInfoConsumeService.selectWorkordersInfoList(workordersId, repStartDate, repEndDate,
				devCategoryCode, devTypeCode, devId, devName, key, devAreaCode, devDeptId, failureTypeCode,
				failureDescrible, repSourceCode, workordersStatusCode, lableClick);
	}

	@SuppressWarnings({ "rawtypes" })
	@GetMapping(value = { "/getWorkordersInfoPage" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("查询维修工单分页信息--维修管理")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repStartDate", value = "报修开始时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repEndDate", value = "报修结束时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devCategoryCode", value = "设备种类编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devTypeCode", value = "设备类型编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devId", value = "设备编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devName", value = "设备名称", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "key", value = "关键字（设备编号或设备名称）", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devAreaCode", value = "设备所属区域", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devDeptId", value = "设备所属部门", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "failureTypeCode", value = "故障类型编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "failureDescrible", value = "故障描述", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repSourceCode", value = "报修来源编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "workordersStatusCode", value = "工单状态编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "orderSeq", value = "排序列名(创建时间CREATE_DATE,截止时间DEADLINE_TIME,结束时间FINISH_TIME)", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "orderMode", value = "排序方式(升序asc,降序desc)", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "lableClick", value = "标签勾选(催办press、延期postpone、逾期overtime、进度反馈fackback、驳回isreject)", 
							paramType = "query", dataType = "String") })
	public ResultPojo getWorkordersInfoPage(
			@RequestParam(defaultValue = "", value = "workordersId", required = false) String workordersId,
			@RequestParam(defaultValue = "", value = "repStartDate", required = false) String repStartDate,
			@RequestParam(defaultValue = "", value = "repEndDate", required = false) String repEndDate,
			@RequestParam(defaultValue = "", value = "devCategoryCode", required = false) String devCategoryCode,
			@RequestParam(defaultValue = "", value = "devTypeCode", required = false) String devTypeCode,
			@RequestParam(defaultValue = "", value = "devId", required = false) String devId,
			@RequestParam(defaultValue = "", value = "devName", required = false) String devName,
			@RequestParam(defaultValue = "", value = "key", required = false) String key,
			@RequestParam(defaultValue = "", value = "devAreaCode", required = false) String devAreaCode,
			@RequestParam(defaultValue = "", value = "devDeptId", required = false) String devDeptId,
			@RequestParam(defaultValue = "", value = "failureTypeCode", required = false) String failureTypeCode,
			@RequestParam(defaultValue = "", value = "failureDescrible", required = false) String failureDescrible,
			@RequestParam(defaultValue = "", value = "repSourceCode", required = false) String repSourceCode,
			@RequestParam(defaultValue = "", value = "workordersStatusCode", required = false) String workordersStatusCode,
			@RequestParam(defaultValue = "", value = "orderSeq", required = false) String orderSeq,
			@RequestParam(defaultValue = "", value = "orderMode", required = false) String orderMode,
			@RequestParam(defaultValue = "", value = "lableClick", required = false) String lableClick,
			Page page, HttpServletRequest request) {
		return workordersInfoConsumeService.selectWorkordersInfoPage(page, workordersId, repStartDate, repEndDate,
				devCategoryCode, devTypeCode, devId, devName, key, devAreaCode, devDeptId, failureTypeCode,
				failureDescrible, repSourceCode, workordersStatusCode, orderSeq, orderMode, lableClick, null);
	}
	
	@SuppressWarnings({ "rawtypes" })
	@GetMapping(value = { "/getWorkordersNoDispatchPage" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("查询维修工单分页信息--运维管理")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repStartDate", value = "报修开始时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repEndDate", value = "报修结束时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devCategoryCode", value = "设备种类编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devTypeCode", value = "设备类型编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devId", value = "设备编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devName", value = "设备名称", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "key", value = "关键字（设备编号或设备名称）", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devAreaCode", value = "设备所属区域", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devDeptId", value = "设备所属部门", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "failureTypeCode", value = "故障类型编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "failureDescrible", value = "故障描述", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repSourceCode", value = "报修来源编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "workordersStatusCode", value = "工单状态编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "orderSeq", value = "排序列名(创建时间CREATE_DATE,截止时间DEADLINE_TIME,结束时间FINISH_TIME)", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "orderMode", value = "排序方式(升序asc,降序desc)", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "lableClick", value = "标签勾选(催办press、延期postpone、逾期overtime、进度反馈fackback、驳回isreject)", 
							paramType = "query", dataType = "String") })
	public ResultPojo getWorkordersNoDispatchPage(
			@RequestParam(defaultValue = "", value = "workordersId", required = false) String workordersId,
			@RequestParam(defaultValue = "", value = "repStartDate", required = false) String repStartDate,
			@RequestParam(defaultValue = "", value = "repEndDate", required = false) String repEndDate,
			@RequestParam(defaultValue = "", value = "devCategoryCode", required = false) String devCategoryCode,
			@RequestParam(defaultValue = "", value = "devTypeCode", required = false) String devTypeCode,
			@RequestParam(defaultValue = "", value = "devId", required = false) String devId,
			@RequestParam(defaultValue = "", value = "devName", required = false) String devName,
			@RequestParam(defaultValue = "", value = "key", required = false) String key,
			@RequestParam(defaultValue = "", value = "devAreaCode", required = false) String devAreaCode,
			@RequestParam(defaultValue = "", value = "devDeptId", required = false) String devDeptId,
			@RequestParam(defaultValue = "", value = "failureTypeCode", required = false) String failureTypeCode,
			@RequestParam(defaultValue = "", value = "failureDescrible", required = false) String failureDescrible,
			@RequestParam(defaultValue = "", value = "repSourceCode", required = false) String repSourceCode,
			@RequestParam(defaultValue = "", value = "workordersStatusCode", required = false) String workordersStatusCode,
			@RequestParam(defaultValue = "", value = "orderSeq", required = false) String orderSeq,
			@RequestParam(defaultValue = "", value = "orderMode", required = false) String orderMode,
			@RequestParam(defaultValue = "", value = "lableClick", required = false) String lableClick,
			Page page, HttpServletRequest request) {
		List<String> noStatusCodeList = new ArrayList<String>();
		noStatusCodeList.add(EnumDicPublicRest.ORD_WAITDISPATCH.getKey());
		noStatusCodeList.add(EnumDicPublicRest.ORD_ISDELETE.getKey());
		return workordersInfoConsumeService.selectWorkordersInfoPage(page, workordersId, repStartDate, repEndDate,
				devCategoryCode, devTypeCode, devId, devName, key, devAreaCode, devDeptId, failureTypeCode,
				failureDescrible, repSourceCode, workordersStatusCode, orderSeq, orderMode, lableClick, noStatusCodeList);
	}

	@PostMapping(value = { "/createWorkordersInfo" }, consumes = { "application/json; charset=UTF-8" }, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("创建维修工单（可包含派发操作信息）")
	public ResultPojo createWorkordersInfo(
			@RequestBody WorkordersInfoDto workordersInfoDto, HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		//是否维修申请默认赋值为false
		workordersInfoDto.setIsRpeApply(false);
		/*String personId = userInfo.getPersonId();
		String personName = userInfo.getPersonName();
		workordersInfoDto.setRepPersonId(personId);
		workordersInfoDto.setRepPersonName(personName);*/
		return workordersInfoConsumeService.insertWorkordersInfo(workordersInfoDto, userInfo.getPersonName());
	}
	

	@PostMapping(value = { "/applyWorkordersInfo" }, consumes = { "application/json; charset=UTF-8" }, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("申请维修工单（必须包含派发、反馈操作信息）")
	public ResultPojo applyWorkordersInfo(
			@RequestBody WorkordersInfoDto workordersInfoDto, HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		//是否维修申请默认赋值为true
		workordersInfoDto.setIsRpeApply(true);
		/*String personId = userInfo.getPersonId();
		String personName = userInfo.getPersonName();
		workordersInfoDto.setRepPersonId(personId);
		workordersInfoDto.setRepPersonName(personName);*/
		return workordersInfoConsumeService.insertWorkordersInfo(workordersInfoDto, userInfo.getPersonName());
	}
	
	@PutMapping(value = { "/updateWorkordersInfo" }, consumes = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("更新维修工单")
    public ResultPojo updateWorkordersInfo(
    		@RequestBody WorkordersInfoDto workordersInfoDto, HttpServletRequest request) {
		String personName = ((UserDto) request.getAttribute("currentUser")).getPersonName();
        return workordersInfoConsumeService.updateWorkordersInfo(workordersInfoDto, personName);
    }
	
	@DeleteMapping(value = { "/deleteWorkordersInfo" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("删除维修工单")
    @ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String")
    public ResultPojo deleteWorkordersInfo(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		HttpServletRequest request) {
		String personName = ((UserDto) request.getAttribute("currentUser")).getPersonName();
        return workordersInfoConsumeService.deleteWorkordersInfo(workordersId, personName);
    }
	
	@GetMapping(value = { "/selectOperationCount" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("获取维修工单各状态数量统计")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String") })
    public ResultPojo selectOperationCount(
    		@RequestParam(defaultValue = "", value = "startTime", required = false) String startTime,
    		@RequestParam(defaultValue = "", value = "endTime", required = false) String endTime,
    		HttpServletRequest request) {
        return workordersInfoConsumeService.selectOperationCount(startTime, endTime);
    }
}
